import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wanandroid_flutter/bean/system_screen_bean.dart';
import 'package:wanandroid_flutter/controller/system_controller.dart';

import '../widgets/article_item.dart';

///体系页面
class SystemPage extends StatelessWidget {
  SystemPage({super.key});

  final systemCtr = Get.put(SystemController());

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      TabBar(
          controller: systemCtr.tabCtr,
          labelPadding:
              const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
          padding: const EdgeInsets.only(top: 35),
          isScrollable: true,
          tabAlignment: TabAlignment.start,
          tabs: systemCtr.tabs.map((element) {
            return Text(element.name.toString(),style: const TextStyle(fontSize: 15,fontWeight: FontWeight.w700),);
          }).toList()),
      Expanded(
          child: TabBarView(
              controller: systemCtr.tabCtr,
              children: systemCtr.tabs.map((element) {
                return Column(children: [
                  secondScreenList(element),
                  Expanded(child: refreshList())
                ]);
              }).toList()))
    ]);
  }

  ///文章列表
  Widget refreshList() {
    return EasyRefresh(
        //嵌套NestedScrollView的时候推荐使用这个MaterialHeader
        controller: systemCtr.controller,
        onRefresh: systemCtr.refreshRequest,
        onLoad: systemCtr.loadMoreRequest,
        child: Obx(() => ListView.builder(
          padding: const EdgeInsets.only(top: 2),
            itemCount: systemCtr.articleList.length,
            itemBuilder: (context, index) {
              var item = systemCtr.articleList[index];
              return ArticleItem(item: item);
            })));
  }

  ///二级筛选菜单横向列表
  Widget secondScreenList(SystemScreenBean element) {
    return Container(
        height: 45,
        alignment: Alignment.topLeft,
        padding: const EdgeInsets.only(left: 5,right: 5,bottom: 6),
        child: ListView.builder(
            shrinkWrap: true,
            scrollDirection: Axis.horizontal,
            itemCount: element.children.length,
            itemBuilder: (context, index) {
              return Padding(
                  padding: const EdgeInsets.only(left: 5, top: 10),
                  child: Obx(() => GestureDetector(
                        child: Container(
                            alignment: Alignment.center,
                            padding: const EdgeInsets.symmetric(horizontal: 10),
                            decoration: BoxDecoration(
                                color: systemCtr.selectIndex.value == index
                                    ? Get.theme.colorScheme.primary
                                    : Colors.grey.shade400,
                                borderRadius:
                                    const BorderRadius.all(Radius.circular(4))),
                            child: Text(element.children[index].name.toString(),
                                style: TextStyle(
                                    fontSize: 12,
                                    color: systemCtr.selectIndex.value == index
                                        ? Colors.white
                                        : Colors.black))),
                        onTap: () {
                          systemCtr.selectIndex.value = index;
                          systemCtr.refreshRequest();
                        },
                      )));
            }));
  }
}
